<?php
namespace product\service;

use herosphp\utils\ArrayUtils;
use product\service\interfaces\IBrandService;
use common\service\CommonService;
use herosphp\core\Loader;

Loader::import('product.service.interfaces.IBrandService');

/**
 * product(Service)接口实现
 * @package product\service
 * @author yangjian<yangjian102621@gmail.com>
 */
class BrandService extends CommonService implements IBrandService {

    public function getItem($condition, $fields, $order)
    {
        $item = parent::getItem($condition, $fields, $order);
        //获取分类
        $productType = Loader::model("productType")->getItem($item['typeid']);
        $item['typename'] = $productType['name'];
        return $item;
    }

    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if ( !empty($items) ) {
            $ids = array();
            foreach ( $items as $val ) {
                $ids[] = $val['typeid'];
            }
            $model = Loader::model("productType");
            $productTypes = $model->getItems(array('id' => array('$in' => $ids)));
            $productTypes = ArrayUtils::changeArrayKey($productTypes, 'id');
            foreach ( $items as $key => $value ) {
                $items[$key]['typename'] = $productTypes[$value['typeid']]['name'];
            }
        }

        return $items;
    }
}
